<template>
    <div>
        <div class="headstyle">
            <el-row :gutter="20">
            <el-col :span="8">
                <div class="grid-content bg-purple">
                <span class="locsty"><i class="el-icon-location iconsty"></i>当前位置：课程管理</span>
                </div>
            </el-col>
            <el-col :span="8">
                <div class="grid-content bg-purple">
                    <el-input  v-model="couname" placeholder="请输入课程名称" clearable></el-input>
                    <el-button type="primary" @click="selectCourse">查询</el-button>
                </div>
            </el-col>
            <el-col :span="8">
                <div class="grid-content bg-purple">
                    <el-button type="primary" @click="goTo('/coursemanage/editcourse')">新增课程</el-button>
                    <!-- <el-button type="danger" @click="deletecourselist(sels)" :disabled="this.sels.length === 0||this.disabled">批量删除</el-button> -->
                </div>
            </el-col>
            </el-row>
        </div>
        <div class="contentstyle">
             <el-table ref="multipleTable"  :data="tableData.filter(data=>total!=0)"  
                 tooltip-effect="dark" 
                 style="width: 100%;text-align:center"
                 border @selection-change="handleSelectionChange">
                <el-table-column type="selection"  width="55" align="center"></el-table-column>
                <el-table-column  prop="rownum" label="序号" width="100" align="center"> </el-table-column>
                <el-table-column  prop="coursename" label="课程名称" show-overflow-tooltip align="center"> </el-table-column>
                <el-table-column  prop="cosrsescope" label="课程组分类" show-overflow-tooltip align="center"> </el-table-column>
                <el-table-column  prop="date" label="创建日期" show-overflow-tooltip align="center"> </el-table-column>
                <!-- <el-table-column  label="日期"   width="120"><template slot-scope="scope">{{ scope.row.Cos_Time }}</template></el-table-column> -->
                <el-table-column  prop="isuse" label="是否可用" show-overflow-tooltip align="center"> </el-table-column>
                <el-table-column label="操作" align="center" width="200">
                    <template slot-scope="scope">
                       <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
                       <el-button  size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    v-if = "paginationShow"
                    @current-change="currentChange"
                    @size-change="sizeChange"
                    :current-page="currentPage"
                    :page-size="pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>
        </div>
    </div>
</template>

<script>
export default {
  data() {
    return {
      couname: "",
      pageSize: 10, // 页容量
      currentPage: 1, // 当前页
      total: 0, // 数据的总条数
      paginationShow: true, // 控制分页的显示隐藏达到刷新的效果
      sels: [], //选中显示的值
      disabled: true,
      //课程数据
      tableData: [
        {
          couid: "",
          rownum: "",
          coursename: "",
          cosrsescope: "",
          date: "",
          isuse: ""
        }
      ],
      search: ""
    };
  },
  //页面初次加载
  mounted: function() {
    this.selectCourse();
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.sels = val;
    },
    //点击链接跳转
    goTo(path) {
      this.$router.replace(path);
    },
    //批量删除
    // deletecourselist(rows) {
    //   var ids = [];
    //   rows.forEach(element => {
    //     ids.push(element.id);
    //   });
    //   this.$confirm("确定要删除选中的文件吗?", "提示")
    //     .then(() => {
    //       $.post("/Course/", {
    //         ids: ids
    //       }).then(dara => {
    //         this.updateData();
    //       });
    //     })
    //     .catch(() => {});
    // },

    // 当页容量发生变化时触发
    sizeChange(size) {
      this.pageSize = size;
      // 重新请求数据
      this.selectCourse();
    },
    // 当页数发生变化时触发
    currentChange(num) {
      this.currentPage = num;
      // 重新请求数据
      this.selectCourse();
    },
    // 查询方法
    searchClicks() {
      this.paginationShow = false;
      this.currentChange(1);
      this.$nextTick(() => {
        this.paginationShow = true;
        this.selectCourse();
      });
    },
    //课程查询方法
    selectCourse() {
      this.$axios
        .get("/Course", {
          params: {
            page: this.currentPage,
            pageSize: this.pageSize,
            couname: this.couname
          }
        })
        .then(json => {
          if (json.data == "logout") {
            this.$message({
              type: "info",
              message: "您的登录信息已失效，请重新登录！"
            });
            this.$router.push("/login");
          } else {
            //this.tableData=JSON.parse(json.data.coulist)
            var courseList = JSON.parse(json.data.coulist);
            this.total = json.data.totalpage;
            var coulistdata = [];
            for (let i = 0; i < courseList.length; i++) {
              var item = {
                couid: courseList[i].Cos_ID,
                rownum: i + 1,
                coursename: courseList[i].Cos_Name,
                date: courseList[i].Cos_Time,
                isuse: courseList[i].Cos_Flag == true ? "可用" : "禁用",
                cosrsescope: courseList[i].Cos_scope == 0 ? "公开课" : courseList[i].Cos_scope == 1 ? "AZ岗" : "非AZ岗"
              };
              coulistdata[i] = item;
            }
            this.tableData = coulistdata;
          }
        });
    },
    //编辑
    handleEdit(index, row) {
      //this.$router.push('/coursemanage/editcourse',{params:{id:row.couid}});
      var id = row.couid;
      this.$router.push({ name: "editcourse", params: { id } });
    },
    //删除，物理删除
    handleDelete(index, row) {
      var id = row.couid;
      this.$confirm(
        "已经被培训的课程关联着相关培训数据，删除操作请慎点，确定要删除选中的文件吗?",
        "提示"
      ).then(() => {
        this.$axios
          .delete("/Course/" + id + "/V5")
          .then(json => {
            if (json.data == 1) {
              this.$message({
                message: "删除成功！",
                type: "success"
              });
              this.selectCourse();
            }
            else{
              this.$message({
                message: "出现异常请重试！",
                type: "error"
              });
            }
          })
          .catch(error => {});
      });
    }
  },
  computed: {
    totalNum() {
      return this.total;
    }
  },
  watch: {
    // 监听总条数,每次查询将当前页置为 1
    totalNum(val) {
      if (val == (this.currentPage - 1) * this.pageSize && val != 0) {
        this.currentPage -= 1;
        this.selectCourse(this);
      }
    }
  }
};
</script>


<style scoped>
.locsty {
  font-size: 16px;
  font-family: "Microsoft YaHei";
}
.editsty {
  padding: 3px;
  font-size: large;
}
.iconsty {
  color: #53b9fe;
  font-size: 22px;
}
.headstyle {
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 20px;
}
.contentstyle {
  text-align: center;
  width: 90%;
  padding: 40px;
}
.el-input {
  width: 200px;
}
</style>

